﻿namespace Chain.DAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class MerchantSite
    {
        public int Add(Chain.Model.MerchantSite model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into MerchantSite(");
            builder.Append("MerchantDesc,MerchantPhoto,MerchantRemark)");
            builder.Append(" values (");
            builder.Append("@MerchantDesc,@MerchantPhoto,@MerchantRemark)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MerchantDesc", SqlDbType.NVarChar, 50), new SqlParameter("@MerchantPhoto", SqlDbType.NVarChar, 200), new SqlParameter("@MerchantRemark", SqlDbType.NVarChar, 500) };
            cmdParms[0].Value = model.MerchantDesc;
            cmdParms[1].Value = model.MerchantPhoto;
            cmdParms[2].Value = model.MerchantRemark;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int MerchantID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MerchantSite ");
            builder.Append(" where MerchantID=@MerchantID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MerchantID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MerchantID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string MerchantIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MerchantSite ");
            builder.Append(" where MerchantID in (" + MerchantIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int MerchantID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MerchantSite");
            builder.Append(" where MerchantID=@MerchantID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MerchantID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MerchantID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select MerchantID,MerchantDesc,MerchantPhoto,MerchantRemark ");
            builder.Append(" FROM MerchantSite ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" MerchantID,MerchantDesc,MerchantPhoto,MerchantRemark ");
            builder.Append(" FROM MerchantSite ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.MerchantID desc");
            }
            builder.Append(")AS Row, T.*  from MerchantSite T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public Chain.Model.MerchantSite GetModel(int MerchantID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 MerchantID,MerchantDesc,MerchantPhoto,MerchantRemark from MerchantSite ");
            builder.Append(" where MerchantID=@MerchantID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MerchantID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MerchantID;
            Chain.Model.MerchantSite site = new Chain.Model.MerchantSite();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["MerchantID"] != null) && (set.Tables[0].Rows[0]["MerchantID"].ToString() != ""))
            {
                site.MerchantID = int.Parse(set.Tables[0].Rows[0]["MerchantID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MerchantDesc"] != null) && (set.Tables[0].Rows[0]["MerchantDesc"].ToString() != ""))
            {
                site.MerchantDesc = set.Tables[0].Rows[0]["MerchantDesc"].ToString();
            }
            if ((set.Tables[0].Rows[0]["MerchantPhoto"] != null) && (set.Tables[0].Rows[0]["MerchantPhoto"].ToString() != ""))
            {
                site.MerchantPhoto = set.Tables[0].Rows[0]["MerchantPhoto"].ToString();
            }
            if ((set.Tables[0].Rows[0]["MerchantRemark"] != null) && (set.Tables[0].Rows[0]["MerchantRemark"].ToString() != ""))
            {
                site.MerchantRemark = set.Tables[0].Rows[0]["MerchantRemark"].ToString();
            }
            return site;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM MerchantSite ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.MerchantSite model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update MerchantSite set ");
            builder.Append("MerchantDesc=@MerchantDesc,");
            builder.Append("MerchantPhoto=@MerchantPhoto,");
            builder.Append("MerchantRemark=@MerchantRemark");
            builder.Append(" where MerchantID=@MerchantID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MerchantDesc", SqlDbType.NVarChar, 50), new SqlParameter("@MerchantPhoto", SqlDbType.NVarChar, 200), new SqlParameter("@MerchantRemark", SqlDbType.NVarChar, 500), new SqlParameter("@MerchantID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.MerchantDesc;
            cmdParms[1].Value = model.MerchantPhoto;
            cmdParms[2].Value = model.MerchantRemark;
            cmdParms[3].Value = model.MerchantID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

